import React, { PureComponent, Fragment } from 'react';
import { Card, Col, Form, Button, Input, Modal, message } from 'antd';
import { connect } from 'dva';
const FormItem = Form.Item;
const { TextArea } = Input;

@Form.create()
@connect()
class RepairAddForm extends PureComponent {
  constructor(props) {
    super(props);
    this.state = {
      visible: false,
    };
    this.user_id = sessionStorage.getItem('user_id');
    this.dispatch = this.props.dispatch;
  }
  submitAddValues = () => {
    this.props.form.validateFields((err, values) => {
      if (!err) {
        const hide = message.loading('正在新增...', 0);
        this.dispatch({
          type: 'repair/fetchAddEquipment',
          payload: {
            // userID:this.user_id,
            userID: 'BFBAAD3E-679F-426F-BF03-05DEE138DBA6',
            model: {
              MaintainCause: values.MaintainCause,
              MaintainContent: values.MaintainContent,
              MaintainAdvice: values.MaintainAdvice,
              EquipmentName: values.EquipmentName,
            },
          },
          callback: response => {
            hide();
            if (response.status) {
              message.success('添加成功');
              this.setState({ visible: false });
              this.props.queryList();
            } else {
              message.error('添加失败');
              console.log(response.ErrorMessage);
            }
          },
        });
      }
    });
  };
  componentDidMount() {
    this.props.OnRef(this, { type: 'AddForm' });
  }
  render() {
    const { getFieldDecorator } = this.props.form;
    const formItemLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 4 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 20 },
      },
    };
    return (
      <Modal
        title="新增"
        destroyOnClose
        visible={this.state.visible}
        onOk={() => {
          this.submitAddValues();
        }}
        onCancel={() => {
          this.setState({ visible: false });
        }}
      >
        <Form {...formItemLayout}>
          <FormItem label="设施名称">
            {getFieldDecorator('EquipmentName', {
              rules: [{ required: true, message: '请填写设施名称！' }],
              initialValue: null,
            })(<Input placeholder="设施名称" />)}
          </FormItem>

          <FormItem label="损坏原因">
            {getFieldDecorator('MaintainCause', { initialValue: null })(
              <TextArea placeholder="损坏原因" rows={4} />
            )}
          </FormItem>
          <FormItem label="维修内容">
            {getFieldDecorator('MaintainContent', { initialValue: null })(
              <TextArea placeholder="维修内容" rows={4} />
            )}
          </FormItem>
          <FormItem label="拟处理意见">
            {getFieldDecorator('MaintainAdvice', { initialValue: null })(
              <TextArea placeholder="拟处理意见" rows={4} />
            )}
          </FormItem>
        </Form>
      </Modal>
    );
  }
}

export default RepairAddForm;
